package com.ksl.kinglockguard.widget;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

import com.ksl.kinglockguard.activity.LockMainActivity;
import com.lsk.open.core.MyLog;

public class GuideGallery extends Gallery {

	private LockMainActivity m_iact;
	public GuideGallery(Context context) {
		super(context);
	}
	
	public GuideGallery(Context context,AttributeSet attrs) {
		super(context, attrs);
	}
	
	public GuideGallery(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	public void setImageActivity(LockMainActivity iact){
		m_iact = iact;
	}
	
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		int kEvent;
        if(isScrollingLeft(e1, e2)){ //Check if scrolling left
          kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
        }
        else{ //Otherwise scrolling right
          kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
        }
        onKeyDown(kEvent, null); 
        return false;  
		
	}
	private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
		 System.out.println(this.getSelectedItemPosition());
        return e2.getX() > e1.getX();
        
    }
	
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return super.onScroll(e1, e2, distanceX, distanceY);
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			this.stopTimer();
			break;
		case MotionEvent.ACTION_MOVE:
			this.stopTimer();
			break;
		case MotionEvent.ACTION_UP:
			this.stopTimer();
			this.startTimer();
			break;
			}
		return super.onTouchEvent(event);
	}
	
	private static final int timerAnimation = 1;
	 private final Handler mHandler = new Handler()
	 {
	  public void handleMessage(android.os.Message msg)
	  {
	   switch (msg.what)
	   {
	   case timerAnimation:
	     onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
//	    }
	      break;
	   default:
	      break;
	   }
	  };
	 };
	 
	 private Timer timer = null;
	 private TimerTask task = null;
	 
	 public void startTimer(){  
	        if (timer == null) {  
	        	timer = new Timer();  
	        }  
	        if (task == null) {  
	        	task = new TimerTask() {  
	                @Override  
	                public void run() {  
	                	mHandler.sendEmptyMessage(timerAnimation);
	              	    MyLog.i("jyh1", "timer starts");
	                }  
	            };  
	        }  
	        if(timer != null && task != null )  
	        	timer.schedule(task, 5000, 5000);  
	  
	    }  
	 
	 public void stopTimer(){  
	        if (timer != null) {  
	        	timer.cancel();  
	        	timer = null;  
	        }  
	        if (task != null) {  
	        	task.cancel();  
	        	task = null;  
	        }     
	    }  
	 
}
